import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useUploadStore = defineStore('upload', () => {
  // 上传任务列表
  const uploadTasks = ref([]);

  // 添加上传任务
  const addUploadTask = (task) => {
    uploadTasks.value.push(task);
  };

  // 更新上传进度
  const updateUploadProgress = (taskId, progress) => {
    const taskIndex = uploadTasks.value.findIndex(task => task.id === taskId);
    if (taskIndex !== -1) {
      uploadTasks.value[taskIndex].progress = progress;
    }
  };

  // 更新上传状态
  const updateUploadStatus = (taskId, status, message = '') => {
    const taskIndex = uploadTasks.value.findIndex(task => task.id === taskId);
    if (taskIndex !== -1) {
      uploadTasks.value[taskIndex].status = status;
      if (message) {
        uploadTasks.value[taskIndex].message = message;
      }
    }
  };

  // 移除上传任务
  const removeUploadTask = (taskId) => {
    uploadTasks.value = uploadTasks.value.filter(task => task.id !== taskId);
  };

  return {
    uploadTasks,
    addUploadTask,
    updateUploadProgress,
    updateUploadStatus,
    removeUploadTask
  };
});